using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.RequestOfficeSupplies.DAL.DAL.Common;
using XQ.RequestOfficeSupplies.DAL.Model.Business;
using XQ.RequestOfficeSupplies.DAL.Model;
using XQ.RequestOfficeSupplies.DAL.Models;
using System.Data.SqlClient;

namespace XQ.RequestOfficeSupplies.DAL.DAL.Business
{
    public class ApplicationFormOfficeSuppliesDal:BaseDal<ApplicationForm_OfficeSupplies>
    {
        public List<ApplicationFormOfficeSuppliesReturnModel> GetList(ApplicationFormOfficeSuppliesSearchModel ssm, Pagination page, TokenModel token)
        {
            using (RequestOfficeSuppliesEntities RequestOfficeSuppliese = new RequestOfficeSuppliesEntities())
            {
                List<SqlParameter> sqlParameters = new List<SqlParameter>();
                //分页信息
                StringBuilder sqlPage = new StringBuilder();
                sqlPage.Append(" select count(1) ");
                sqlPage.Append(" from ApplicationFormOfficeSupplies afos");
                sqlPage.Append(" where 1=1 ");

                ////企业名称
                //if (!string.IsNullOrEmpty(ssm.enterprisenName))
                //{
                //    sqlPage.Append(" and c.enterprisenName like '%" + ssm.enterprisenName + "%'");
                //}
                ////联系人
                //if (!string.IsNullOrEmpty(ssm.contacts))
                //{
                //    sqlPage.Append(" and c.contacts like '%" + ssm.contacts + "%'");
                //}
                ////联系电话
                //if (!string.IsNullOrEmpty(ssm.contactsPhone))
                //{
                //    sqlPage.Append(" and c.contactsPhone like '%" + ssm.contactsPhone + "%'");
                //}
                ////咨询内容
                //if (!string.IsNullOrEmpty(ssm.consultationContent))
                //{
                //    sqlPage.Append(" and c.consultationContent like '%" + ssm.consultationContent + "%' ");
                //}
                ////咨询日期起
                //if (ssm.StartDate.Year != 1970)
                //{
                //    sqlPage.Append(" and c.addTime >= '" + ssm.StartDate + "'");
                //}
                ////咨询日期止
                //if (ssm.EndDate.Year != 9999)
                //{
                //    sqlPage.Append(" and c.addTime <= '" + ssm.EndDate.ToString("yyyy-MM-dd") + " 23:59:59'");
                //}

                int allCount = RequestOfficeSuppliese.Database.SqlQuery<int>(sqlPage.ToString()).First();
                page.RecordCount = allCount;
                page.PageCount = (int)Math.Ceiling((decimal)page.RecordCount / (decimal)ssm.pageSize);
                page.PageIndex = ssm.index;
                page.PageSize = ssm.pageSize;

                //数据列表
                StringBuilder sql = new StringBuilder();
                sql.Append("select top " + ssm.pageSize + " * from ");
                sql.Append(" (select row_number() over(order by afos.id desc) as rowNum, ");
                sql.Append(" afos.*");
                sql.Append(" from ApplicationFormOfficeSupplies afos ");
                sql.Append(" where 1=1 ");
                ////企业名称
                //if (!string.IsNullOrEmpty(ssm.enterprisenName))
                //{
                //    sql.Append(" and c.enterprisenName like '%" + ssm.enterprisenName + "%'");
                //}

                ////咨询内容
                //if (!string.IsNullOrEmpty(ssm.consultationContent))
                //{
                //    sql.Append(" and c.consultationContent like '%" + ssm.consultationContent + "%' ");
                //}
                ////咨询日期起
                //if (ssm.StartDate.Year != 1970)
                //{
                //    sql.Append(" and c.addTime >= '" + ssm.StartDate + "'");
                //}
                ////咨询日期止
                //if (ssm.EndDate.Year != 9999)
                //{
                //    sql.Append(" and c.addTime <= '" + ssm.EndDate.ToString("yyyy-MM-dd") + " 23:59:59'");
                //}
                sql.Append(") t1 ");
                sql.Append("where rowNum>" + (ssm.index - 1) * ssm.pageSize);
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "获取申请表物品关联列表SQL", "", "", "", sql.ToString());
                return RequestOfficeSuppliese.Database.SqlQuery<ApplicationFormOfficeSuppliesReturnModel>(sql.ToString()).ToList();
            }
        }
    }
}
